package com.tianfeng.huayi.controller;

import com.alipay.api.AlipayApiException;
import com.tianfeng.huayi.entity.AlipayRequest;
import com.tianfeng.huayi.entity.HousingInfo;
import com.tianfeng.huayi.entity.Property;
import com.tianfeng.huayi.service.IPropertyService;
import com.tianfeng.huayi.util.AlipayTradePrecreate;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/property")
@CrossOrigin
public class PropertyController {

    @Autowired
    private IPropertyService propertyService;

    /**
     * 查询房产信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> list(@RequestBody Property property)
    {
        Map<Object,Object> response = new HashMap<>();
        List<Property> propertyList = propertyService.selectPropertyList(property);
        if (!propertyList.isEmpty()) {
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", propertyList);
        } else {
            response.put("code", 400);
            response.put("msg", "查询失败");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据房产ID查询房产信息列表
     */
    @PostMapping("/selectPropertyByPropertyId")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> selectPropertyByPropertyId(@RequestBody Property property)
    {
        Map<Object,Object> response = new HashMap<>();
        Property selected = propertyService.selectPropertyByPropertyId(property.getPropertyId());
        if (selected != null) {
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", selected);
        } else {
            response.put("code", 400);
            response.put("msg", "查询失败");
        }
        return ResponseEntity.ok(response);
    }


    /**
     * 生成支付宝付款二维码
     */
    @PostMapping("/alipayQrcode")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> alipayQrcode(@RequestParam(name = "subject") String subject, @RequestParam(name = "price") String price) throws AlipayApiException {
        Map<Object, Object> response = new HashMap<>();
        // 生成UUID作为流水号
        String flowingWater = UUID.randomUUID().toString().replace("-", "");
        ResponseEntity<Map<Object, Object>> mapResponseEntity = AlipayTradePrecreate.aliPayQrCode(subject, flowingWater, price);

        System.out.println(mapResponseEntity.getBody());
        response.put("qrcode", mapResponseEntity.getBody());
        response.put("flowingWater", flowingWater);
        return ResponseEntity.ok(response);
    }

    /**
     * 查询是否扫码支付成功
     */
    @PostMapping("/confirmAlipay")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> confirmAlipay(@RequestParam(name = "flowingWater") String flowingWater) throws AlipayApiException {
        Map<Object, Object> response = new HashMap<>();
        ResponseEntity<Map<Object, Object>> mapResponseEntity = AlipayTradePrecreate.confirmAlipay(flowingWater);
        if (Objects.requireNonNull(mapResponseEntity.getBody()).get("code").equals(200)) {
            System.out.println(mapResponseEntity.getBody());
        }
        response.put("data", mapResponseEntity.getBody());
        return ResponseEntity.ok(response);
    }

    /**
     * 充值水电费
     */
    @PostMapping("/recharge")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> recharge (@RequestBody Property property) {
        Map<Object,Object> response = new HashMap<>();
        int i = propertyService.updateProperty(property);
        if (i != 0) {
            response.put("code", 200);
            response.put("msg", "修改成功");
            Property property1 = propertyService.selectPropertyByPropertyId(property.getPropertyId());
            response.put("data", property1);
        } else {
            response.put("code", 400);
            response.put("msg", "修改失败");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据户型ID查询户型信息
     */

    @PostMapping("/selectHousingByHousingId")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> selectHousingByHousingId(@RequestBody HousingInfo housingInfo) {
        Map<Object,Object> response = new HashMap<>();
        HousingInfo housingInfo1 = propertyService.selectHousingByHousingId(housingInfo.getHousingId());
        if (housingInfo1 != null) {
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", housingInfo1);
        } else {
            response.put("code", 400);
            response.put("msg", "查询失败");
        }
        return ResponseEntity.ok().body(response);
    }


}
